package controller;

import pojo.User;
import service.LoginManagement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: zen
 * @date: 2020/4/12
 * @Title: LoginCheck
 * @Description:
 */

@WebServlet("/loginCheck")
public class LoginCheck extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        String account = req.getParameter("account");
        String pass = req.getParameter("secret");
        // 登录成功则把用户写入会话
        if (LoginManagement.login(account, pass)) {
            out.write("ok");
            User user = LoginManagement.getUser(account);
            session.setAttribute("user", user);
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
